/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.plugins.videoplayer;

import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;

import com.zyuan.lawyerport.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 播放器
 */
public class PlyerActivity extends AppCompatActivity {
    @BindView(R.id.videoView)
    VideoView videoView;
//    @BindView(R.id.videoplayer)
//    JzvdStd videoplayer;

//    @BindView(R.id.detail_player)
//    StandardGSYVideoPlayer detailPlayer;

    private String url = "";

    private String[] urlArray = {"/DCIM/1.mp4", "/DCIM/2.mp4", "/DCIM/3.mp4"};
    private int iPlayer = 0;//播放的第几个视频

    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
//            Toast.makeText( PlyerActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
            if (iPlayer < 2) {
                iPlayer++;
            } else if (iPlayer == 2) {
                iPlayer = 0;
            }

            initPlayerVideo();//播放
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //去掉标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        hideBottomUIMenu();
        //设置全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏显示
        }
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_plyer);
        ButterKnife.bind(this);


        initPlayerVideo();//播放


    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }


    private void initPlayerVideo() {

        url = Environment.getExternalStorageDirectory() + urlArray[iPlayer];
        Uri uri = Uri.parse(url);
//        //设置视频控制器
//        videoView.setMediaController(new MediaController(this));
        //播放完成回调
        videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());
        //设置视频路径
        videoView.setVideoURI(uri);
        //开始播放视频
        videoView.start();
    }


}
